import pgzrun
from vec import *
import random
import time

WIDTH = 600
HEIGHT = 440

BACK_COLOR = (0, 0, 0)
PEN_COLOR = (0, 200, 0)

lastTime = time.time()
deltaTime = 0

rcHeight = [109, 208, 127, 86, 55, 154, 23, 162, 91, 130]
n = len(rcHeight)
rcWidth = 20
space = 10
pspd = 0.5

buckets = [0] * 201

class MyRect:
    def __init__(self, lb_x, lb_y, h, clr):
        self.pos = Vec2(lb_x, lb_y)
        self.h = h
        self.clr = clr
        self.p = 0.0
        self.curState = 0   # 0: 静止，1: 移动
        self.start_pt = Vec2(0, 0)
        self.end_pt = Vec2(0, 0)

    def StartMove(self, end_pt):
        if self.curState == 1:
            return

        self.curState = 1
        self.p = 0
        self.start_pt = self.pos
        self.end_pt = end_pt

    def Move(self):
        has_finished = False

        self.p += pspd * deltaTime

        if self.p > 1.0:    
            self.p = 1.0
            has_finished = True

        self.pos = self.start_pt * (1 - self.p) + self.end_pt * self.p

        return has_finished

    def Update(self):
        if self.curState == 1:
            if self.Move() == True:
                self.curState = 0

    def Draw(self):
        rc = Rect((self.pos.x, self.pos.y - self.h), (rcWidth, self.h))
        screen.draw.filled_rect(rc, self.clr)

startX = 150
iter = startX
rcList = []

isSortingFinished = False
sortingState = 0 # 0: 交换数据，1: 移动矩形

i = 0
max_num = max(rcHeight) # 最大值
j = len(str(max_num))   # 记录最大值的位数

for item in rcHeight:
    r = random.randint(50, 200)
    g = random.randint(50, 200)
    b = random.randint(50, 200)
    rcList.append(MyRect(iter, HEIGHT / 2, item, (r, g, b)))
    iter += rcWidth
    iter += space

def update():
    global lastTime, deltaTime, isSortingFinished, sortingState, rcHeight, rcList, i, j
    curTime = time.time()
    deltaTime = curTime - lastTime
    lastTime = curTime

    if isSortingFinished == True:
        return

    if sortingState == 0:
        if i >= j:
            isSortingFinished = True
        else:
            bucket_list =[[] for _ in range(10)] #初始化桶数组
            for x in rcHeight:
                bucket_list[int(x / (10**i)) % 10].append(x) # 找到位置放入桶数组
            s = []
            for x in bucket_list:   # 放回原序列
                for y in x:
                    s.append(y)
            
            rclst = []
            for k in range(len(s)):
                idx = rcHeight.index(s[k])
                rcList[idx].StartMove(rcList[k].pos)
                rclst.append(rcList[idx])
            
            rcHeight = s
            rcList = rclst
            i += 1

            sortingState = 1
    else:
        allStoped = True
        for rc  in rcList:
            rc.Update()
            if rc.curState == 1:
                allStoped = False

        if allStoped == True:
            sortingState = 0

def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    screen.draw.line((0, HEIGHT / 2), (WIDTH, HEIGHT / 2), PEN_COLOR)
    for rc in rcList:
        rc.Draw()

pgzrun.go()